#include "CurrentvsLengthSimul.h"

#include "MatrixSave.h"
#include "TrajSimul.h"
#include "InitialPosition.h"
#include "FinalPosition.h"
#include "SwitchingCheck.h"

#include <iostream>

CurrentvsLengthSimul::CurrentvsLengthSimul()
{

}

CurrentvsLengthSimul::~CurrentvsLengthSimul()
{

}

MultiLayer CurrentvsLengthSimul::solve(Clock clock,Output output,Pulse *field,Pulse *current,MultiLayer multiLayer,NewParameters params)
{
 output.setSave(false);
 int layerNumber=multiLayer.getSolveLayer();
 MultiLayer result=multiLayer;
 multiLayer[layerNumber].setMag(InitialPosition(clock,output,field,current,multiLayer,params));
 multiLayer[layerNumber].setSwitchedMag(FinalPosition(clock,output,field,current,multiLayer,params));
 std::cout<<multiLayer[layerNumber].getMag().getX()<<"=="<<multiLayer[layerNumber].getMag().getY()<<"=="<<multiLayer[layerNumber].getMag().getZ()<<std::endl;
 std::cout<<multiLayer[layerNumber].getSwitchedMag().getX()<<"=="<<multiLayer[layerNumber].getSwitchedMag().getY()<<"=="<<multiLayer[layerNumber].getSwitchedMag().getZ()<<std::endl;
 for(current->setLevel(params.getCurrent()["min"]);current->getLevel(clock.getTimeStamp())<=params.getCurrent()["max"];current->setLevel(current->getLevel(clock.getTimeStamp())+params.getCurrent()["step"]))
    {
      for(current->setLength(params.getCurrentPulse()["min"]);current->getLength()<=params.getCurrentPulse()["max"];current->setLength(current->getLength()+params.getCurrentPulse()["step"]))
	{
	  std::cout<<current->getLength()<<std::endl;
	  double nswitch=0;
	  int nrun=1;
	  if(params.getTemperature()!=0)
	    nrun=params.getNrun();
	  for(int counter=0;counter<nrun;counter++)
	    {
	      result=TrajSimul().solve(clock,output,field,current,multiLayer,params);
	      if(SwitchingCheck(result[multiLayer.getSolveLayer()],params.getTorque()["tolerance"]).getCheck())
		nswitch++;
	    }
	  nswitch/=nrun;
	  MatrixSave().doublesave(output,result[layerNumber].getMag().getZ());
	}
      //to go to a new row      
      MatrixSave().stringsave(output,"\n");

    }
  return multiLayer;
}

